//
//  SQPlayVoiceView.m
//  energy
//
//  Created by 周曼迪 on 2021/7/23.
//

#import "SQPlayVoiceView.h"

@implementation SQPlayVoiceView


- (instancetype)init{
    self = [super initWithFrame:CGRectMake(0, 0, W(250), W(35))];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self buildView];
    }
    return self;
}


-(void)buildView{
    UIView *voice_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, W(250), W(35))];
    [voice_view SQCornerRadius:7];
    voice_view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    [self addSubview:voice_view];
    // yuyin_icon
    UIImage *yuyin_icon = [UIImage imageNamed:@"voice"];
    UIImageView *yuyin_icon_imgview = [[UIImageView alloc] initWithImage:yuyin_icon];
    yuyin_icon_imgview.frame = CGRectMake(12.5, 5, 25, 25);
    [voice_view addSubview:yuyin_icon_imgview];
    
    CGFloat sub_width = voice_view.width-55;
    CGFloat item_with = sub_width/30.f;
    for (int i = 0; i < 30; i++) {
        CGFloat height = 0;
        if (i<15) {
            height = 10+i;
        }else{
            height = 25-i;
        }
        UIView *item_veiw = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yuyin_icon_imgview.frame)+5+i*item_with, (voice_view.height-height)/2.f, item_with-2, height)];
        item_veiw.backgroundColor = [UIColor colorNamed:@"notColor"];
        [item_veiw SQCornerRadius:(item_with-2)/2.f];
        [voice_view addSubview:item_veiw];
    }
    // 播放语音按钮
    UIButton *play_record_btn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yuyin_icon_imgview.frame)+5, 0, voice_view.width-(CGRectGetMaxX(yuyin_icon_imgview.frame)+5)-30, voice_view.height)];
//    play_record_btn.userInteractionEnabled = NO;
    play_record_btn.backgroundColor = [UIColor clearColor];
    play_record_btn.alpha = 0.5;
    [play_record_btn addTarget:self action:@selector(play_record_btn_action:) forControlEvents:UIControlEventTouchUpInside];
    [voice_view addSubview:play_record_btn];
    self.play_record_btn = play_record_btn;
}

- (void)buildPlayVoiceVie:(NSString *)urlStr{
    self.voiceUrl = urlStr;
  
    
   
    
}

// 播放录音
- (void)play_record_btn_action:(UIButton *)sender{
    
    sender.selected = !sender.selected;

    NSString *hdr_audio = [NSString stringWithFormat:@"%@%@",LC_ImageHeader,self.voiceUrl ];
    if (![hdr_audio CheckNillWithString]) {
        if (sender.selected == YES) {
            [[NewPlayerManager manager] playNetMusic:[NSString stringWithFormat:@"%@%@",LC_ImageHeader,self.self.voiceUrl ]];
        }else{
            [[NewPlayerManager manager] playerPause];
        }
    }
    
    
    
//    [[ShlRecordManager manager] playRecord];
   
    
}

@end
